package com.atheima.reggie.controller;

import com.atheima.reggie.common.R;
import com.atheima.reggie.common.ValidateUtil;
import com.atheima.reggie.entity.Employee;
import com.atheima.reggie.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody Employee employee){




        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username",employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        if(emp == null) {
            return R.error("用户名不存在");
        }
        if(!password.equals(emp.getPassword())){
            return R.error("密码错误");
        }
        if(emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        request.getSession().setAttribute("employeeId",emp.getId());
        return R.ok().put("data",emp);
    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employeeId");
        return R.ok("退出成功");
    }

    @PostMapping("")
    public R addEmployee(HttpServletRequest request, @RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long employeeId = (Long) request.getSession().getAttribute("employeeId");
        employee.setCreateUser(employeeId);
        employee.setUpdateUser(employeeId);
        employeeService.save(employee);
        return R.ok("成功");
    }

    @PutMapping("")
    public R updateEmployee(HttpServletRequest request, @RequestBody Employee employee) {


        employee.setUpdateTime(LocalDateTime.now());
        Long employeeId = (Long) request.getSession().getAttribute("employeeId");

        employee.setUpdateUser(employeeId);
        employeeService.updateById(employee);

        log.info("controller update方法中线程id: {}",Thread.currentThread().getId());
        return R.ok("成功");
    }

    @GetMapping ("/page")
    public R page(int page,int pageSize,String name) {
        Page<Employee> employeePage = new Page<>(page, pageSize);
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        if(ValidateUtil.isNotEmpty(name)) {
            employeeQueryWrapper.like("name",name);
        }
        employeeService.page(employeePage, employeeQueryWrapper);
        return R.ok("成功").put("data",employeePage);
    }

    @GetMapping ("/{id}")
    public R page(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);


        return R.ok("成功").put("data",employee);
    }
}
